home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10706 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.5 KB

  1. Path: ix.netcom.com!news
  2. From: mjudge@ix.netcom.com(Michael Judge )
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: question assignment operator in c++
  5. Date: 9 Mar 1996 17:23:04 GMT
  6. Organization: Netcom
  7. Message-ID: <4hsepo$kpu@dfw-ixnews4.ix.netcom.com>
  8. References: <menghua_wang.1.002DB7BE@muccmail.missouri.edu>
  9. NNTP-Posting-Host: bos-ma9-01.ix.netcom.com
  10. X-NETCOM-Date: Sat Mar 09 11:23:04 AM CST 1996
  11.  
  12. In <menghua_wang.1.002DB7BE@muccmail.missouri.edu>
  13. menghua_wang@muccmail.missouri.edu (menghua wang) writes: 
  14. >
  15. >I do not why the assignment operator can only be overloaded as a
  16. member 
  17. >function in a class such as:
  18. >
  19. >          class myclass {
  20. >              int i;
  21. >          public:
  22. >              myclass operator= (myclass &A) {
  23. >                 i = A.i;
  24. >                 return *this;
  25. >              }
  26. >          };
  27. >
  28. >Why does not the following frind overloading work?
  29. >
  30. >              mycalss operator= (myclass &A, myclass &B) {
  31. >                   A.i = B.i;
  32. >                   return A;
  33. >              }
  34. >
  35. >thank you for your help
  36. >
  37.  
  38. Ok. your problem is that you are not returning a reference to myclass.
  39. In the first example, you have added an operator = to the class and so
  40. allowing a copy constructor to be made.  the friend function is trying
  41. to make a new myclass to return and is failing to find the
  42. myclass::operator= since you took it out.  change the second to
  43.  
  44. myclass &operator=(myclass &A,myclass &B){
  45. A.i=B.i;
  46. return A
  47. }
  48.  
  49. that should be fine.  If not, sue me.
  50. Michael Judge
  51. mjudge@ix.netcom.com
  52.  
  53.